home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / DebugLib / DebugLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-20  |  6.1 KB  |  222 lines  |  [TEXT/KAHL]

  1. /* debug utilities
  2.     94/01/20 aih - updated debug window to use new document library
  3.     94/01/06 aih - added debug window, removed THINK C console window
  4.     93/12/20 aih - added memory display windoid
  5.     93/12/04 aih - added debugger command
  6.     93/03/18 aih - added console show/hide command
  7.     93/03/14 aih - created */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "DebugLib.h"
  12. #include "MacLib.h"
  13. #include "ResourceConstantsLib.h"
  14.  
  15. extern int _profile, _trace; /* profiler flags */
  16.  
  17. static void DebugDocumentOpen(void);
  18.  
  19. /*----------------------------------------------------------------------------*/
  20. /* debug menu */
  21. /*----------------------------------------------------------------------------*/
  22.  
  23. static Boolean enabled(void)
  24. {
  25.     return(_assert.require && _assert.ensure && _assert.check);
  26. }
  27.  
  28. void DebugAdjustMenu(void)
  29. {
  30.     if (GetMHandle(RLM_DEBUG)) {
  31.         MenuCmdEnable(CMD_DEBUG);
  32.         MenuCmdEnable(CMD_DEBUG_WINDOW);
  33.         if (MacHasDebugger()) MenuCmdEnable(CMD_DEBUG_DEBUGGER);
  34.         #if ASSERT_REQUIRE || ASSERT_ENSURE || ASSERT_CHECK
  35.             MenuCmdEnable(CMD_DEBUG_ASSERT_ALL);
  36.             #if ASSERT_REQUIRE
  37.                 MenuCmdEnable(CMD_DEBUG_ASSERT_REQUIRE);
  38.                 MenuCmdCheck(CMD_DEBUG_ASSERT_REQUIRE, _assert.require);
  39.             #endif
  40.             #if ASSERT_ENSURE
  41.                 MenuCmdEnable(CMD_DEBUG_ASSERT_ENSURE);
  42.                 MenuCmdCheck(CMD_DEBUG_ASSERT_ENSURE, _assert.ensure);
  43.             #endif
  44.             #if ASSERT_CHECK
  45.                 MenuCmdEnable(CMD_DEBUG_ASSERT_CHECK);
  46.                 MenuCmdCheck(CMD_DEBUG_ASSERT_CHECK, _assert.check);
  47.             #endif
  48.             MenuCmdEnable(CMD_DEBUG_ASSERT_BREAK);
  49.             MenuCmdEnable(CMD_DEBUG_ASSERT_RAISE);
  50.             MenuCmdEnable(CMD_DEBUG_ASSERT_STACK);
  51.             MenuCmdCheck(CMD_DEBUG_ASSERT_ALL, enabled());
  52.             MenuCmdCheck(CMD_DEBUG_ASSERT_BREAK, _assert.debug);
  53.             MenuCmdCheck(CMD_DEBUG_ASSERT_RAISE, _assert.raise);
  54.             MenuCmdCheck(CMD_DEBUG_ASSERT_STACK, _assert.stack);
  55.         #endif /* ASSERT_REQUIRE || ASSERT_ENSURE || ASSERT_CHECK */
  56.         #if ASSERT_TRACE
  57.             MenuCmdEnable(CMD_DEBUG_ASSERT_TRACE);
  58.             MenuCmdCheck(CMD_DEBUG_ASSERT_TRACE, _assert.trace);
  59.         #endif
  60.         #if PROFILE
  61.             MenuCmdEnable(CMD_DEBUG_TRACE);
  62.             MenuCmdEnable(CMD_DEBUG_PROFILE);
  63.             MenuCmdCheck(CMD_DEBUG_TRACE, _trace);
  64.             MenuCmdCheck(CMD_DEBUG_PROFILE, _profile);
  65.         #endif
  66.     }
  67. }
  68.  
  69. Boolean DebugMenu(const MenuPickType *pick)
  70. {
  71.     Boolean handled = true;
  72.     
  73.     switch (pick->cmd) {
  74.     case CMD_DEBUG_DEBUGGER:
  75.         DebugStr((StringPtr) "\p Test Application");
  76.         break;
  77.     case CMD_DEBUG_WINDOW:
  78.         DebugDocumentOpen();
  79.         break;
  80.     case CMD_DEBUG_ASSERT_ALL:
  81.         _assert.require = _assert.ensure = _assert.check = ! enabled();
  82.         break;
  83.     case CMD_DEBUG_ASSERT_REQUIRE:
  84.         _assert.require = ! _assert.require;
  85.         break;
  86.     case CMD_DEBUG_ASSERT_ENSURE:
  87.         _assert.ensure = ! _assert.ensure;
  88.         break;
  89.     case CMD_DEBUG_ASSERT_CHECK:
  90.         _assert.check = ! _assert.check;
  91.         break;
  92.     case CMD_DEBUG_ASSERT_BREAK:
  93.         _assert.debug = ! _assert.debug;
  94.         break;
  95.     case CMD_DEBUG_ASSERT_RAISE:
  96.         _assert.raise = ! _assert.raise;
  97.         break;
  98.     case CMD_DEBUG_ASSERT_TRACE:
  99.         _assert.trace = ! _assert.trace;
  100.         break;
  101.     case CMD_DEBUG_ASSERT_STACK:
  102.         _assert.stack = ! _assert.stack;
  103.         break;
  104.     case CMD_DEBUG_TRACE:
  105.         _trace = ! _trace;
  106.         break;
  107.     case CMD_DEBUG_PROFILE:
  108.         _profile = ! _profile;
  109.         break;
  110.     default:
  111.         handled = false;
  112.         break;
  113.     }
  114.     return(handled);
  115. }
  116.  
  117. /*----------------------------------------------------------------------------*/
  118. /* debug window */
  119. /*----------------------------------------------------------------------------*/
  120.  
  121. #include "ApplicationLib.h"
  122. #include "ApplicationPreferencesLib.h"
  123. #include "PreferencesLib.h"
  124. #include "ResourceLib.h"
  125. #include "TextDocumentLib.h"
  126. #include "WindowLib.h"
  127.  
  128. static DocumentHandle gDebugDocument; /* document for displaying debug messages */
  129.  
  130. /* handler called by document library to close the debug window */
  131. static void DebugDocumentEnd(DocumentHandle doc)
  132. {
  133.     require(doc == gDebugDocument);
  134.     TxDocEnd(gDebugDocument);
  135.     gDebugDocument = NULL;
  136.     ensure(! gDebugDocument);
  137. }
  138.  
  139. /* open the debug window */
  140. static void DebugDocumentOpen(void)
  141. {
  142.     FileNameType name;                /* name of debug log file */
  143.     FileType file, *fp = &file;    /* debug log file */
  144.     TextPrefsType *prefs = NULL;    /* for setting prefs for new document */
  145.     TextPrefsType svprefs;            /* for saving original prefs settings */
  146.     volatile DocumentHandle doc = NULL; /* for creating the debug document */
  147.     static DocumentTableType table;/* for overriding document's end handler */
  148.     
  149.     TRY {
  150.         if (! gDebugDocument) {
  151.         
  152.             /* create debug file if it doesn't already exist */
  153.             ResStrLen(RLS_FILE, RLS_FILE_DEBUG, name, sizeof(FileNameType));
  154.             FileSetPref(fp, name);
  155.             if (! FileExists(fp))
  156.                 FileCreate(fp, AppCreator(), 'TEXT');
  157.             
  158.             /* change preferences so document will use the fastest version of
  159.                 a text document (an unstyled document without word wrap) */
  160.             prefs = &AppPrefs()->text;
  161.             svprefs = *prefs;
  162.             prefs->kind = TX_UNSTYLED_KIND; /* program_note: due to a bug in TE32K can't use TX_EXTENDED_KIND */
  163.             prefs->wrap = false;
  164.             
  165.             /* Create the document, setting the autosave flag so it will
  166.                 automatically be saved when closed or when we quit. */
  167.             doc = DocBegin(fp);
  168.             DocAutoSaveSet(doc, true);
  169.             
  170.             /* Override the end handler so that we can clear the gDebugDocument
  171.                 global when the document is closed. */
  172.             table = *DocTable(doc);
  173.             table.end = DebugDocumentEnd;
  174.             DocTableSet(doc, &table);
  175.             gDebugDocument = doc;
  176.             
  177.             WinShow(DocWindow(doc));
  178.         }
  179.         WinSelect(DocWindow(gDebugDocument));
  180.     } CLEANUP {
  181.         if (prefs)
  182.             *prefs = svprefs;
  183.     } CATCH {
  184.         DocEnd(doc);
  185.     } ENDTRY;
  186.     ensure(WinFirstVisible(WinLayer(DocWindow(gDebugDocument))) ==
  187.                 DocWindow(gDebugDocument));
  188. }
  189.  
  190. /* print the string to the debug window */
  191. void DebugVPrintf(const char *fmt, va_list ap)
  192. {
  193.     TextScrollHandle scrl;
  194.     TextHandle text;
  195.     char out[512];
  196.     int i, length;
  197.     
  198.     length = vsprintf(out, fmt, ap);
  199.     check(0 <= length && length < sizeof(out));
  200.     for (i = 0; i < length; i++) {
  201.         if (out[i] == '\n')
  202.             out[i] = '\r';
  203.     }
  204.     if (! gDebugDocument)
  205.         DebugDocumentOpen();
  206.     scrl = TxDocTextScroll(gDebugDocument);
  207.     text = TxScrlText(scrl);
  208.     TxSelEndSet(text, TxLength(text));
  209.     TxSelStartSet(text, TxLength(text));
  210.     TxInsert(text, out, length, NULL);
  211.     TxScrlChanged(scrl);
  212. }
  213.  
  214. void DebugPrintf(const char *fmt, ...)
  215. {
  216.     va_list ap;
  217.     
  218.     va_start(ap, fmt);
  219.     DebugVPrintf(fmt, ap);
  220.     va_end(ap);
  221. }
  222.